﻿using System;
using System.Data;
using BLL.Access;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.ZhangWuGL
{
    public partial class FeiShuiJK : BasePage
    {
        #region 变量

        private readonly SpHelper _sphelper = new SpHelper();

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{

            //}
            InitData();
            ScriptManager1.RegisterPostBackControl(btnShengChengWJ);
        }

        #endregion

        #region 属性

        /// <summary>
        ///     保存结果集
        /// </summary>
        private DataTable dtSourceWSC
        {
            get { return ViewState["dtSourceWSC"] as DataTable; }
            set { ViewState["dtSourceWSC"] = value; }
        }

        /// <summary>
        ///     保存结果集
        /// </summary>
        private DataTable dtSourceYSC
        {
            get { return ViewState["dtSourceYSC"] as DataTable; }
            set { ViewState["dtSourceYSC"] = value; }
        }

        #endregion

        #region 方法

        /// <summary>
        ///     加载数据
        /// </summary>
        private void InitData()
        {
            dtSourceWSC = _sphelper.P_ZWFS_GETDATE(0, 0).Tables[0];
            gvWeiShangC.DataSource = dtSourceWSC;
            gvWeiShangC.DataBind();
            dtSourceYSC = _sphelper.P_ZWFS_GETDATE(1, 0).Tables[0];
            gvYiShangC.DataSource = dtSourceYSC;
            gvYiShangC.DataBind();
        }


        ///// <summary>
        ///// 响应前台回调
        ///// </summary>
        //protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        //{
        //    string user = LoginInfo.CurrentUser.UserID.ToString();
        //    string msg = "";
        //    if (e.Parameter == "scsj")
        //    {
        //        int zwym = Convert.ToInt32(txtZhangWuNY.Text.Trim());
        //        DataTable dt = _sphelper.P_ZWFS_GETDATE(2, zwym).Tables[0];
        //        if (dt.Rows.Count > 0)
        //        {
        //            if (dt.Rows[0]["I_YISHANGC"].ToString() == "1")
        //            {
        //                msg = "1";
        //            }
        //            else if (dt.Rows[0]["I_YISHANGC"].ToString() == "0")
        //            {
        //                msg = "0";
        //            }
        //        }
        //    }
        //    else if (e.Parameter == "scsj2")//生成数据
        //    {
        //        int zwym = Convert.ToInt32(txtZhangWuNY.Text.Trim());
        //        DataTable dt = _sphelper.P_QSZW_SCSJ(zwym.ToString(), user).Tables[0];
        //        if (dt.Rows.Count > 0)
        //        {
        //            msg = dt.Rows[0][0].ToString();
        //        }
        //    }
        //    else if (e.Parameter == "scwj")//生成文件
        //    {
        //        int wjbh = Convert.ToInt32(this.hdCollection.Get("ID"));
        //        DataTable dt = _sphelper.P_QSZW_SCWJ(wjbh, 1).Tables[0];
        //        if (dt.Rows.Count > 0)
        //        {
        //            DataTable dt2 = _sphelper.P_QSZW_SCWJ(wjbh, 3).Tables[0];
        //            string wjm = dt2.Rows.Count > 0 ? dt2.Rows[0][0].ToString() : "" ;
        //            //生成文件
        //            PageTools.CreateTxt(dt, wjm);
        //            //if (dt.Rows.Count > 0)
        //            //{
        //            //    msg = "0";
        //            //}

        //        }
        //    }
        //    else if (e.Parameter == "qswj")//取消生成文件
        //    {
        //        int wjbh = Convert.ToInt32(this.hdCollection.Get("ID"));
        //        DataTable dt = _sphelper.P_QSZW_SCWJ(wjbh, 0).Tables[0];
        //        if (dt.Rows.Count > 0)
        //        {
        //            msg = dt.Rows[0][0].ToString();
        //        }
        //    }

        //    e.Result = e.Parameter + "|0|" + msg;
        //}
        //#endregion

        //protected void gvWeiShangC_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        //{
        //    this.dtSourceWSC = _sphelper.P_ZWFS_GETDATE(0, 0).Tables[0];
        //    this.gvWeiShangC.DataSource = this.dtSourceWSC;
        //    this.gvWeiShangC.DataBind();
        //}

        //protected void gvYiShangC_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        //{
        //    this.dtSourceYSC = _sphelper.P_ZWFS_GETDATE(1, 0).Tables[0];
        //    this.gvYiShangC.DataSource = this.dtSourceYSC;
        //    this.gvYiShangC.DataBind();
        //}


        /// <summary>
        ///     生成文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnShengChengWJ_Click(object sender, EventArgs e)
        {
            var wjbh = Convert.ToInt32(gvWeiShangC.GetRowValues(gvWeiShangC.FocusedRowIndex, "I_BIANHAO"));
            var dt = _sphelper.P_QSZW_SCWJ(wjbh, 1).Tables[0];
            if (dt.Rows.Count > 0)
            {
                var dt2 = _sphelper.P_QSZW_SCWJ(wjbh, 3).Tables[0];
                var wjm = dt2.Rows.Count > 0 ? dt2.Rows[0][0].ToString() : "";
                if (wjm != "")
                {
                    //生成文件
                    PageTools.CreateTxt(dt, wjm);
                    InitData();
                }
                else
                {
                    PageTools.AlertMsg(this, "请选择需要生成的文件");
                }
            }
        }

        /// <summary>
        ///     取消生成文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuXiaoWJ_Click(object sender, EventArgs e)
        {
            var wjbh = Convert.ToInt32(gvYiShangC.GetRowValues(gvYiShangC.FocusedRowIndex, "I_BIANHAO"));
            var dt = _sphelper.P_QSZW_SCWJ(wjbh, 0).Tables[0];
            if (dt.Rows.Count > 0)
                if (dt.Rows[0][0].ToString() == "0")
                {
                    PageTools.AlertMsg(this, "取消成功！");
                    InitData();
                }
        }

        /// <summary>
        ///     生成数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnShengChengSJ_Click(object sender, EventArgs e)
        {
            var user = LoginInfo.CurrentUser.UserID.ToString();
            //验证1
            if (txtZhangWuNY.Text.Trim() == "")
            {
                PageTools.AlertMsg(this, "账务年月为空，请输入！");
                return;
            }

            var zwym = Convert.ToInt32(txtZhangWuNY.Text.Trim());
            var dt = _sphelper.P_ZWFS_GETDATE(2, zwym).Tables[0];
            if (dt.Rows.Count > 0)
                if (dt.Rows[0]["I_YISHANGC"].ToString() == "1")
                {
                    PageTools.AlertMsg(this, "此帐务年月已经上传,不允许重新生成数据！");
                    return;
                }

            var dt2 = _sphelper.P_QSZW_SCSJ(zwym.ToString(), user).Tables[0];
            if (dt2.Rows.Count > 0)
            {
                var msg = dt2.Rows[0][0].ToString();
                if (msg == "0")
                {
                    PageTools.AlertMsg(this, "生成数据成功！");
                    InitData();
                }
                else if (msg == "1")
                {
                    PageTools.AlertMsg(this, "没有可生成的数据");
                }
                else if (msg == "2")
                {
                    PageTools.AlertMsg(this, "有发票为空的到帐信息");
                }
            }
        }

        #endregion
    }
}